package com.iris.live.services.common.reconsitution;

import com.google.gson.JsonObject;
import com.iris.live.services.common.JPush;
import com.iris.live.services.common.JsonObjectUtils;
import com.iris.live.services.common.LshMsgUtils;
import com.iris.live.services.common.StringUtils;
import com.iris.live.services.data.models.*;
import com.iris.live.services.data.repositories.Lsh库存表Repository;
import com.iris.live.services.data.repositories.MessageSettingRepository;
import com.iris.live.services.data.repositories.用户Repository;
import com.iris.live.services.data.repositories.顾客记录汇总Repository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

/**
 * yitianle
 * 指定角色发送 message
 * 2017/3/8
 */
@Component
public class SendMsgRoleUtil {

    private static SendMsgRoleUtil sendMsgRoleUtil;
    //日志
    private Logger logger = LoggerFactory.getLogger(SendMsgRoleUtil.class);

    @Autowired
    private MessageSettingRepository messageSettingRepository;
    @Autowired
    private Lsh库存表Repository lsh库存表Repository;
    @Autowired
    private 用户Repository repository用户;
    @Autowired
    private 顾客记录汇总Repository 顾客记录汇总Repository;



    /**
     * 销售行政（通知和推送）
     */
    public static void  sendMsgBySaleAdmin(){

    }


    /**
     * 销售顾问（通知和推送）
     * @param lsh库存表Model
     * @param flow
     * @param model报价单表
     * @param quotedPriceCodeOrVin
     * @param cus
     * @param content
     * @param entrance
     * @param json 推送自定义格式
     */
    public static void  sendMsgBySale(Lsh库存表Model lsh库存表Model,PushApprovalWorkFlowModel flow,
                                      报价单表Model model报价单表,String quotedPriceCodeOrVin,
                                      顾客记录汇总Model cus,String content,Integer entrance,JsonObject json){
        //入库
        LshMsgUtils.getLshMsg(flow.getPermit(), flow.getUserName(), flow.getPermit(), model报价单表.get销售顾问(), MessageType.Type.getName(MessageType.MSG_TYPE_CONTRACT),
                content, flow.getUserName(), cus.get电话号码());

        //推送
        List appList = new ArrayList();
        appList.add(model报价单表.get销售顾问());
        if(StringUtils.isNullOrBlank(json)){
            JPush.sendPushForAndroidAndIos(content, content,
                    JsonObjectUtils.getJsonObject(flow.getType().toString(), "2", quotedPriceCodeOrVin, "",
                            "", model报价单表.get销售顾问()),
                    appList, JPush.Platforms.iTask);
        }else{
            JPush.SendPushITaskForAndroidAndIos(content, content, json, appList);
        }
    }

    /**
     * 行政给销售顾问推送
     *
     *
     * @param model报价单表
     * @param cus
     * @param content
     * @param json 推送自定义格式
     */
    public static void  sendMsgBySale( String sendUsername,
                                      报价单表Model model报价单表,
                                      顾客记录汇总Model cus,String content,JsonObject json){
        //入库
        LshMsgUtils.getLshMsg(model报价单表.get许可(), sendUsername, model报价单表.get许可(), model报价单表.get销售顾问(), MessageType.Type.getName(MessageType.MSG_TYPE_ADMINISTRATIVE),
                content,sendUsername, cus.get电话号码());

        //推送
        List appList = new ArrayList();
        appList.add(model报价单表.get销售顾问());
        if(StringUtils.isNullOrBlank(json)){
            JPush.sendPushForAndroidAndIos(content, content,
                    JsonObjectUtils.getJsonObject(null, "2", model报价单表.get报价编号().toString(), "",
                            "", model报价单表.get销售顾问()),
                    appList, JPush.Platforms.iTask);
        }else{
            JPush.SendPushITaskForAndroidAndIos(content, content, json, appList);
        }
    }


    /**
     * 财务专员(只有通知)
     * @param permit 许可
     * @param sender 发送人
     * @param mesType 消息分类
     * @param stockNum 库存编号
     * @param cusNum 顾客编号
     * @param content 消息文本 不传content 默认会按传入stockNum和cusNum构建
     * @param roleType 消息设置的分类编号
     * @param checkValue 是否需要资源确认
     */
    @Async
    public static void  sendMsgByFinance(String permit,String sender,String mesType,
                                         Integer stockNum,Integer cusNum,String content,
                                         String roleType,String checkValue,String mobile ){
        //根据库存编号查询车架号
        String carNo ="";
        String cusTel ="";
        String cusName ="";
        if (!StringUtils.isNullOrBlank(stockNum)){
            Lsh库存表Model stock = sendMsgRoleUtil.lsh库存表Repository.findLshStockByStockId(stockNum);
            if(null != stock){
                carNo = stock.get车架号();
            }
        }
        //根据顾客编号查询名称电话
        if (!StringUtils.isNullOrBlank(cusNum)){
            顾客记录汇总Model cusModel = sendMsgRoleUtil.顾客记录汇总Repository.findCusByCustomerID(cusNum);
            if (null != cusModel){
                cusTel = cusModel.get电话号码();
                cusName = cusModel.get顾客姓名();
            }
        }
        String defaultContent= content;
        if (StringUtils.isNullOrBlank(content)){
            defaultContent= MessageContext.getCheckCarSource(carNo,cusName,cusTel);
        }

        if (StringUtils.isNullOrBlank(mobile)){
            mobile= cusTel;
        }

        String recName =getFanceName(permit,roleType,checkValue);
        if (!StringUtils.isNullOrBlank(recName)) {
            //入库
            LshMsgUtils.getLshMsg(permit,
                    sender,
                    permit,
                    recName,
                    mesType,
                    defaultContent,
                    sender,
                    mobile);
        }else{
            //log提醒设置资源确认财务专员
            sendMsgRoleUtil.logger.warn("请先在PC审批设置面板设置面板中，设置资源确认消息接收相关的财务专员！");
        }
    }

    /**
     * 发送消息给对应消息的所有角色
     * @param permit 许可
     * @param sender 发送人
     * @param mesType 消息分类
     * @param content 消息文本
     * @param role  接收人角色
     */
    @Async
    public static void  sendMsgToAllRole(String permit,String sender,String mesType,
                                        String content,
                                         String role ,String mobile ){

        List<String> getALLName = getALLName(  permit,  role );
          if(getALLName!=null)  {
            //入库
        for(String recName:getALLName) {
            LshMsgUtils.getLshMsg(permit,
                    sender,
                    permit,
                    recName,
                    mesType,
                    content,
                    sender,
                    mobile);
        }}
    }

    /**
     * 获取设定的财务名称(邮箱)
     * @param permit
     * @param roleType
     * @param checkValue
     * @return
     */
    public static String getFanceName(String permit,String roleType, String checkValue){
        String  name ="";
        //查询消息接收设定中的车资确认相关行政
        MessageSettingModel settingModelName = sendMsgRoleUtil.messageSettingRepository.findByPermitAndType(permit,roleType,checkValue);
        if(null !=settingModelName){
            用户Model m财务 = sendMsgRoleUtil.repository用户.findByPermitAndUserName(permit, DefaultValueConstant.SYS_ROLE_SALEADMIN, settingModelName.getUser());
            if (m财务 != null) {
                name = m财务.get用户名();
            }
        }
        return name;
    }
    /**
     *
     */
    public static List<String> getALLName(String permit,String roleType){
          List<String> getFanceName=new ArrayList<>();
         String  name;
            List<用户Model>   modelList = sendMsgRoleUtil.repository用户.findBy角色And许可(permit,roleType);
            if (modelList != null) {
                for(用户Model m:modelList){
                    name = m.get用户名();
                    getFanceName.add(name);
                }
            }
        return getFanceName;
    }

    /**
     * 获取设定的财务名称（用户名）
     * @param permit
     * @param roleType
     * @param checkValue
     * @return
     */
    public static String getFanceUserName(String permit,String roleType, String checkValue){
        String  name ="";
        //查询消息接收设定中的车资确认相关财务
        MessageSettingModel settingModelName = sendMsgRoleUtil.messageSettingRepository.findByPermitAndType(permit,roleType,checkValue);
        if(null !=settingModelName){
            用户Model m财务 = sendMsgRoleUtil.repository用户.findByPermitAndUserName(permit, DefaultValueConstant.SYS_ROLE_SALEADMIN, settingModelName.getUser());
            if (m财务 != null) {
                name = m财务.get人员姓名();
            }
        }
        return name;
    }



    public MessageSettingRepository getMessageSettingRepository() {
        return messageSettingRepository;
    }

    public void setMessageSettingRepository(MessageSettingRepository messageSettingRepository) {
        this.messageSettingRepository = messageSettingRepository;
    }

    public Lsh库存表Repository getLsh库存表Repository() {
        return lsh库存表Repository;
    }

    public void setLsh库存表Repository(Lsh库存表Repository lsh库存表Repository) {
        this.lsh库存表Repository = lsh库存表Repository;
    }

    public 用户Repository getRepository用户() {
        return repository用户;
    }

    public void setRepository用户(用户Repository repository用户) {
        this.repository用户 = repository用户;
    }

    public com.iris.live.services.data.repositories.顾客记录汇总Repository get顾客记录汇总Repository() {
        return 顾客记录汇总Repository;
    }

    public void set顾客记录汇总Repository(com.iris.live.services.data.repositories.顾客记录汇总Repository 顾客记录汇总Repository) {
        this.顾客记录汇总Repository = 顾客记录汇总Repository;
    }

    @PostConstruct
    public void init() {
        sendMsgRoleUtil = this;
        sendMsgRoleUtil.repository用户 = this.repository用户;
        sendMsgRoleUtil.messageSettingRepository = this.messageSettingRepository;
        sendMsgRoleUtil.顾客记录汇总Repository = this.顾客记录汇总Repository;
        sendMsgRoleUtil.lsh库存表Repository = this.lsh库存表Repository;

    }

}
